package cn.tedu.medicalmanagementsystem.manager.service.impl;

import cn.tedu.medicalmanagementsystem.base.exception.ServiceException;
import cn.tedu.medicalmanagementsystem.base.response.StatusCode;
import cn.tedu.medicalmanagementsystem.manager.mapper.ManagerAppointmentMapper;
import cn.tedu.medicalmanagementsystem.manager.pojo.dto.AppointmentAddParam;
import cn.tedu.medicalmanagementsystem.manager.pojo.dto.AppointmentDelParam;
import cn.tedu.medicalmanagementsystem.manager.pojo.dto.AppointmentListQuery;
import cn.tedu.medicalmanagementsystem.manager.pojo.dto.AppointmentUpdateParam;
import cn.tedu.medicalmanagementsystem.manager.pojo.entity.Appointment;
import cn.tedu.medicalmanagementsystem.manager.pojo.vo.AppointmentVO;
import cn.tedu.medicalmanagementsystem.manager.service.ManagerAppointmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class ManagerAppointmentServiceImpl implements ManagerAppointmentService {

    @Autowired
    ManagerAppointmentMapper managerAppointmentMapper;

    // 添加预约
    @Override
    public AppointmentVO add(AppointmentAddParam appointmentAddParam) {
        log.debug("开始处理添加预约业务，参数: {}", appointmentAddParam);

        Appointment appointment = new Appointment();
        BeanUtils.copyProperties(appointmentAddParam,appointment);
        managerAppointmentMapper.insert(appointment);

        return managerAppointmentMapper.selectById(appointment.getId());
    }

    // 查询预约
    @Override
    public List<AppointmentVO> allAppointments(){
        return managerAppointmentMapper.allAppointments();
    }

    @Override
    public List<AppointmentVO> getByUserId(Long userId) {
        return managerAppointmentMapper.getByUserId(userId);
    }

    @Override
    public AppointmentVO getById(Long id) {
        return managerAppointmentMapper.selectById(id);
    }

    @Override
    public List<AppointmentVO> getByRealName(String realName) {
        return managerAppointmentMapper.selectByRealName(realName);
    }

    @Override
    public List<AppointmentVO> select(AppointmentListQuery appointmentListQuery) {
        return managerAppointmentMapper.select(appointmentListQuery);
    }

    @Override
    public List<AppointmentVO> getByContactRealName(String contactName) {
        return managerAppointmentMapper.getByContactRealName(contactName);
    }

//    @Override
//    public List<AppointmentVO> selectAppointment(AppointmentListQuery appointmentListQuery) {
//        List<AppointmentVO> list = managerAppointmentMapper.selectAppointment(appointmentListQuery);
//        log.debug("查询用户业务:userQuery={}",appointmentListQuery);
//        return list;
//    }


    //  更新预约
    @Override
    public AppointmentVO update(AppointmentUpdateParam appointmentUpdateParam) {
        log.debug("开始处理更新预约业务，参数: {}", appointmentUpdateParam);
        if (appointmentUpdateParam.getId() == null || appointmentUpdateParam.getId() <= 0) {
            throw new ServiceException(StatusCode.DATA_UNEXISTS);
        }

        if (managerAppointmentMapper.selectById(appointmentUpdateParam.getId()) == null) {
            throw new ServiceException(StatusCode.DATA_UNEXISTS);
        }

        Appointment appointment = new Appointment();
        BeanUtils.copyProperties(appointmentUpdateParam,appointment);

        managerAppointmentMapper.update(appointment);

        log.debug("更新后的数据: {}", appointment);
        return managerAppointmentMapper.selectById(appointment.getId());
    }

    @Override
    public int delete(AppointmentDelParam appointmentDelParam) {
        log.debug("开始处理删除预约业务，参数: {}",appointmentDelParam);
        managerAppointmentMapper.deleteById(appointmentDelParam.getId());
        return 1;
    }

}
